﻿@page "/lesson/add/{MainPlanId}"

@inject IJSRuntime JsRuntime
@inject NavigationManager NavigationManager
@inject ILessonService LessonService
@inject IMainPlanService MainPlanService

<PageTitle>新增经验教训</PageTitle>
<h3>
    @_mainPlanDto.Number-@_mainPlanDto.Name
    <a href="lessons/mainplan/@_mainPlanDto.Id">[查看项目经验教训]</a>
</h3>

<EditForm Model="_lessonDto" OnSubmit="Save">
    <div class="input-group mb-2">
        <span class="input-group-text">经验内容</span>
        <textarea class="form-control" placeholder="请详细描述经验教训，可输入多行" style="height: 180px" @bind="@_lessonDto.Content"></textarea>
    </div>
    <ImagesUpload Title="经验图片(可添加多个)" @bind-MultiImageUrl="@_lessonDto.ContentUrl" />

    <CompSaveCancelButtons CancelUrl="@($"lessons/mainplan/{_lessonDto.MainPlanId}")" />
</EditForm>
@code {
    [Parameter]
    public string MainPlanId { get; set; }
    
    private LessonDto _lessonDto = new();
    private MainPlanDto _mainPlanDto = new();

    protected override async Task OnInitializedAsync()
    {
        var mResult = await MainPlanService.GetFirstOrDefaultAsync(Guid.Parse(MainPlanId));
        _mainPlanDto = mResult.Result;
        _lessonDto.MainPlanId=Guid.Parse(MainPlanId);
    }

    private async Task Save()
    {
        if (string.IsNullOrEmpty(_lessonDto.Content))
        {
            await JsRuntime.InvokeVoidAsync("alert", "填写的信息不完整");
            return;
        }
        var result = await LessonService.AddAsync(_lessonDto);
        if (result.IsSuccessStatusCode) NavigationManager.NavigateTo($"lessons/mainplan/{MainPlanId}");
    }
}
